package com.example.lockspringbootstarter.lock.manager;

import com.example.lockspringbootstarter.dao.SdkDistributeLockDao;
import com.example.lockspringbootstarter.lock.*;
import io.etcd.jetcd.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

public class LockFactory {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    // @Autowired
    // private ZooKeeper zooKeeper;

    @Autowired
    private SdkDistributeLockDao sdkDistributeLockDao;

    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;

    @Autowired
    private Client etcdClient;

    public final static String REDIS = "redis";
    public final static String ZOOKEEPER = "zookeeper";
    public final static String MYSQL = "mysql";
    public final static String ETCD = "etcd";

    public AbstractLock getLock(String lockType, String lockName) {
        switch (lockType) {
            case REDIS:
                return new RedisLock(stringRedisTemplate, lockName);
            // case ZOOKEEPER:
            //     return new ZookeeperLock(zooKeeper, lockName);
            case MYSQL:
                return new MysqlLock(sdkDistributeLockDao, lockName, dataSourceTransactionManager);
            case ETCD:
                return new EtcdLock(etcdClient, lockName);
            default:
                return null;
        }
    }
}